<script>
   function delete_item(link){
      pk = $(link).attr('id');
      $.ajax({
         type: "DELETE",
         url: "/chirping/",
         data: "pk=" + pk,
         success: function (result) {
            window.location = '/chirpings'
         },
         error : function() {
         }
      });
   }

function thumb(link, argv){
   pk = $(link.target).attr('id')
   input_score = $('input[type=text][id=' + pk + ']');
   score = new Number(input_score.val());
   score += argv;
   $.ajax({
      type: "POST",
      url: "/chirping/score",
      data: "pk=" + pk + "&score=" + score,
      success: function (result) {
         //window.location = '/chirpings'
         input_score.val(score)
      },
      error : function() {
      }
   });
}

function thumb_up(link){
   thumb(link, 1)
}
function thumb_down(link){
   thumb(link, -1)
}

window.onload = function(){
   render_timestamp();

   menu = $('ul.nav>li.chirpings');
   menu.addClass('active');
   menu_link = $('ul.nav>li.chirpings>a');
   menu_link.attr('href', '#');

   $('button.delete').each(function(index){
      link = this
      $(link).on('click', function(){
         delete_item(link)
      });
   });

   $('a.thumb-up').on('click', thumb_up);
   $('a.thumb-down').on('click', thumb_down);
}
</script>
